/*
 * Copyright 2009-2010 by The Regents of the University of California
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may obtain a copy of the License from
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.ucr.cs.dblab.acestore.base.datatypes;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author jarodwen
 * @created May 15, 2011
 */
public class FloatSerDeserializer implements ISerDeserializer<Float> {

    private FloatSerDeserializer(){};
    
    
    
    private static class SingletonHolder {
        public static final FloatSerDeserializer INSTANCE = new FloatSerDeserializer();
    }
    
    public static FloatSerDeserializer getSingleton(){
        return SingletonHolder.INSTANCE;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    public Float deserialize(ByteBuffer buf, int offset, int length)
            throws IOException {
        return deserialize(buf.array(), offset, length);
    }

    @Override
    public Float deserialize(byte[] buf, int offset, int length)
            throws IOException {
        return Float.intBitsToFloat(IntegerSerDeserializer.DESERIALIZE(buf, offset));
    }

    @Override
    public Float deserialize(DataInput in) throws IOException {
        return in.readFloat();
    }

    @Override
    public Float deserialize(String str) throws IOException {
        return Float.valueOf(str);
    }

    @Override
    public byte[] serialize(Float data) throws IOException {
        return null;
    }

    @Override
    public byte[] serialize(String str) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bout);
        out.writeFloat(deserialize(str));
        return bout.toByteArray();
    }

    @Override
    public void serialize(Float data, DataOutput out) throws IOException {
        out.writeFloat(data);
    }

    @Override
    public int getTypeIndex() {
        return 1;
    }

    @Override
    public int getTypeSize() {
        return Float.SIZE / 8;
    }

    @Override
    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
            throws IOException {
        float v1 = deserialize(b1, s1, l1);
        float v2 = deserialize(b2, s2, l2);
        return Float.compare(v1, v2);
    }

    @Override
    public int compare(Float value1, Float value2) {
        return Float.compare(value1, value2);
    }

}
